/*
* @(#)ListDmsVersionAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/

package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsValidation;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.DocumentValidateManager;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;

/**
 * <p>Class Name:       ListDmsDocumentAction.java    </p>
 * <p>Description:      The list action class for ListUserRecord.jsp</p>
 *
 *  @author             Zoe Shum
 *  @company            DCIVision Limited
 *  @creation date      01/08/2003
 *  @version            $Revision: 1.20 $
 */

public class ListDmsVersionAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.20 $";


  /**
   *  Constructor - Creates a new instance of ListDmsDocumentAction and define the default listName.
   */
  public ListDmsVersionAction() {
    super();
    this.setListName("dmsVersionList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsVersionDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_PUBLIC_FOLDER);
  }

  /**
   * execute
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @return
   * @throws ServletException
   */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    ListDmsVersionForm actionForm = (ListDmsVersionForm)form;
    
    // Set function code if necessary.
    if (!Utility.isEmpty(actionForm.getCurFunctionCode())) {
      this.setFunctionCode(actionForm.getCurFunctionCode());
    }
    

    //Add By Jim Zhou       2007/01/10       EIP-1292
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    Integer documentID = TextUtility.parseIntegerObj(actionForm.getDocumentID());
    DmsDocument document = docRetrievalManager.getDocument(documentID);
    DocumentValidateManager docValidateManager = new DocumentValidateManager(sessionContainer, conn);
    StringBuffer message = new StringBuffer();

    DmsValidation validation = docValidateManager.validateOperateAction(document, false, "Z");
    List lstIdMisRight = validation.getLstIdReject();
    List lstIdBeLocked = validation.getLstIdlocked();
    List lstIdBeDeleted = validation.getLstIdDeleted();
    List lstIdBeArchived = validation.getLstIdArchived();
    List lstIdRelationship = validation.getLstIdHaveRelationship();
    if (!lstIdMisRight.isEmpty() ||
        !lstIdBeDeleted.isEmpty() ||
        !lstIdBeArchived.isEmpty()){
      for (int i=0; i<lstIdMisRight.size(); i++) {
        this.addError(request,"errors.dms.no_permission",docRetrievalManager.getLocationPath((Integer)lstIdMisRight.get(i)));
      }
      for (int i=0; i<lstIdBeLocked.size(); i++) {
        this.addError(request,"errors.dms.been_checkout",docRetrievalManager.getLocationPath((Integer)lstIdBeLocked.get(i)));
      }
      for (int i=0; i<lstIdBeDeleted.size(); i++) {
        this.addError(request,"errors.dms.been_deleted",docRetrievalManager.getLocationPath((Integer)lstIdBeDeleted.get(i)));
      }
      for (int i=0; i<lstIdBeArchived.size(); i++) {
        this.addError(request,"errors.dms.been_archived",docRetrievalManager.getLocationPath((Integer)lstIdBeArchived.get(i)));
      }
      if (Utility.isEmpty(actionForm.getReturnTo())){
        return mapping.findForward("Home");
      } else {
        ActionForward actionForward = new ActionForward();
        actionForward.setPath("/dms/" + actionForm.getReturnTo());
        return actionForward;
      }
    }
    //Add ENd

    return super.execute(mapping, form, request, response);
  }

  /**
   * getListData
   *
   * Override the parent's function. load the document versio list and its details.
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void getListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    ListDmsVersionForm verSearchForm = (ListDmsVersionForm)form;
//    SessionContainer sessionContainer = this.getSessionContainer(request);
//    Connection conn = this.getConnection(request);
    super.getListData(mapping, verSearchForm, request, response);
  }

}
